import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/home.vue'
import Login from '../views/login.vue';
import Work from '../views/home/work.vue'
import Role from '../views/home/role/role.vue'
import Roleadd from '../views/home/role/roleadd.vue'
import User from '../views/home/user/user.vue'//用户列表
import Black from '../views/home/user/black.vue'//用户黑名单
import Classadd from '../views/home/class/classadd.vue'//班级添加
import Classlist from '../views/home/class/classlist.vue'//班级列表
import Classsituation from '../views/home/class/classsituation.vue'//班级情况
import Student from '../views/home/student/student.vue'// 学生列表
import Studentadd from '../views/home/student/studentadd.vue'// 学生添加
import  School  from '../views/home/school/school.vue'//学院设置
import Classroom from '../views/home/Classroom/Classroom.vue'//课堂管理
import Template from '../views/home/Template/Template.vue'
import Recordingscreen from '../views/home/Recordingscreen/Recordingscreen.vue'
import axios from 'axios'
//缺少录屏管理
Vue.use(VueRouter)

  const routes = [
  {
    path: '/home',
    name: 'Home',
    component: Home,     
    meta:{
      isLogin:true
    },
    children:[
      {
        path:'/work',
        name:'Work',
        component:Work
      },
      {
        path:'/home',
        redirect:'/work'
      },
      {
        path:'/role',
        name:'Role',
        component:Role
      },
      {
        path:'/roleadd',
        name:'Roleadd',
        component:Roleadd
      },
      {
        path:'/user',
        name:'user',
        component:User
      },
      {
        path:'/black',
        name:'black',
        component:Black
      },
      {
        path:'/classadd',
        name:'Classadd',
        component:Classadd
      },
      {
        path:'/classlist',
        name:'Classlist',
        component:Classlist
      },
      {
        path:'/classsituation',
        name:'Classsituation',
        component:Classsituation
      },
      {
        path:'/student',
        name:'Student',
        component:Student
      },
      {
        path:'/studentadd',
        name:'Studentadd',
        component:Studentadd
      },
      {
        path:'/school',
        name:'School',
        component:School
      },
      {
        path:'/classroom',
        name:'Classroom',
        component:Classroom
      },
      {
        path:'/template',
        name:'Template',
        component:Template
      },{
        path:'/recordingscreen',
        name:'Recordingscreen',
        component:Recordingscreen
      }
    ]
  },
  {
    path: '/login',
    name: 'login',
    component: Login
  },
  {
    path:'/',
    redirect:'/login'
  }

]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}
router.beforeEach((to,from,next)=>{

 if(to.matched.some(item=>item.meta.isLogin)){
 
      axios.get('/islogin').then(data=>{
        
        if(data.data.code===1){
            next()
            return 
        }
        next('/login')
        return 
      })  
  }
  next()
})

export default router
